import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   redirect:'/login'
  // },
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/home',
    name:'name',
    component: () => import('@/views/home'),
    redirect:'/welcome',
    children:[
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('@/views/welcome')
      },
      {
        path:'/users',
        name:'users',
        component:() => import('@/views/user/Users.vue')
      },
      {
        path:'/roles',
        name:'roles',
        component:() => import('@/views/superSuPro/roles.vue')
      },
      {
        path:'/rights',
        name:'rights',
        component:() => import('@/views/superSuPro/rights.vue')
      },
      {
        path:'/goods',
        name:'goods',
        component:() => import('@/views/shopManage/shopList.vue')
      },
      {
        path:'/params',
        name:'params',
        component:() =>import('@/views/shopManage/sortingParameter.vue')
      },
      {
        path:'/categories',
        name:'categories',
        component:() =>import('@/views/shopManage/shopCategory.vue')
      },
      {
        path:'/orders',
        name:'orders',
        component:() =>import('@/views/orders')
      },
      {
        path:'/reports',
        name:'reports',
        component:() =>import('@/views/reports')
      },
        ]
  },
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  if (to.path === '/') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/')
  // console.log(to, '11111111111111111111111')
  // console.log(123456, '')
  next()
})
export default router
